/**
 * zoneland.net Inc.
 * Copyright (c) 2002-2012 All Rights Reserved.
 */
package com.sanerzone.smscenter.gateway.util;

import com.sanerzone.smscenter.gateway.ums.MoListener;
import com.sanerzone.smscenter.gateway.ums.comm.PMessage;
import com.sanerzone.smscenter.gateway.ums.comm.cmpp.CMPPSMProxy;
import com.sanerzone.smscenter.gateway.ums.comm.cmpp.message.CMPPDeliverMessage;
import com.sanerzone.smscenter.gateway.ums.comm.cmpp.message.CMPPSubmitMessage;
import com.sanerzone.smscenter.gateway.ums.message.factory.CMPPMsgFactory;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicLong;

/**
 * 
 * @author gang
 * @version $Id: CmppT.java, v 0.1 2012-8-26 上午6:56:48 gang Exp $
 */
public class CmppT {
	public static AtomicLong cnt = new AtomicLong();
	private static long lastNum = 0;

	public static void main(String[] args) throws IOException {
		Map<String, String> params = new HashMap<String, String>();
//		params.put("host", "192.168.1.180");
//		params.put("port", "7890");
//		params.put("local-host", null);
//		params.put("local-port", null);
//		params.put("read-timeout", "3");
//		params.put("reconnect-interval", "2");
//		params.put("transaction-timeout", "3");
//		params.put("source-addr", "044022");
//		params.put("shared-secret", "0555");
//		params.put("version", "33");
//		params.put("debug", "true");
		
//		gateWay.setId("1");
//    	gateWay.setHost("211.140.12.43");
//    	gateWay.setPort(7890);
//    	gateWay.setSourceAddr("40303");
//    	gateWay.setSharedSecret("Zsmas40303");
//    	gateWay.setVersion("20");
//    	gateWay.setType("CMPP");
//    	gateWay.setCorpId("911337");

	/*	String account = "333";
		//Password.
		String password = "0555";
		//Service code.
		String serviceCode = "9999";
		//Enterprise code. enterpriseCode
		String enterpriseCode = "044022";
		String apiUrl = "127.0.0.1";*/

		/*SID：qx0001
		帐户：qx0001
		密码：qx0001
		接入码：106509673161524
		服务代码：2RTCwXhsk
		对接服务器：112.35.10.164
		端口：1990
		模式：S*/
		params.put("host", "112.35.10.164");
		params.put("port", "1990");
		params.put("local-host", null);
		params.put("local-port", null);
		params.put("read-timeout", "3");
		params.put("reconnect-interval", "2");
		params.put("transaction-timeout", "3");
		params.put("source-addr", "qx0001");
		params.put("shared-secret", "qx0001");
		params.put("version", "20");
		params.put("debug", "true");
		params.put("msg_Src", "qx0001");

		/*params.put("host", "127.0.0.1");
		params.put("port", "7890");
		params.put("local-host", null);
		params.put("local-port", null);
		params.put("read-timeout", "3");
		params.put("reconnect-interval", "2");
		params.put("transaction-timeout", "3");
		params.put("source-addr", "123457");
		params.put("shared-secret", "1111");
		params.put("version", "20");
		params.put("debug", "true");
		params.put("msg_Src", "123457");*/


/*
		params.put("host", "192.168.1.180");
		params.put("port", "7890");
		params.put("local-host", null);
		params.put("local-port", null);
		params.put("read-timeout", "3");
		params.put("reconnect-interval", "2");
		params.put("transaction-timeout", "3");
		params.put("source-addr", "40303");
		params.put("shared-secret", "Zsmas40303");
		params.put("version", "20");
		params.put("debug", "true");
		params.put("msg_Src", "911337");
*/
		final CMPPSMProxy cmppProxy = new CMPPSMProxy("id", params);
		class MyMoListenter implements MoListener {

			public void OnTerminate() {
				System.out.println("服务端断开连接。");
			}

			/**
			 */

			public PMessage onReport(PMessage msg) {
				 CMPPDeliverMessage deliver = (CMPPDeliverMessage)msg;
				 System.out.println("======"+deliver.toString());
				return null;
			}

			/**
			 *      java.util.Map)
			 */

			public void onDeliver(PMessage msg, Map<String, String> args) {
				CMPPDeliverMessage deliver = (CMPPDeliverMessage) msg;
				System.out.println("-------------------------");
			}
		}
		;
		cmppProxy.setMoListener(new MyMoListenter());
		//15967238888
		CMPPMsgFactory fac = new CMPPMsgFactory();
		try {
			List<CMPPSubmitMessage> list = fac.buildCMPPSubmitMessage("2RTCwXhsk","106904540303", new String[] {"18268846802" },
					"你的验证码是124578", 8, 3, params);
			Iterator<CMPPSubmitMessage> itMsg = list.iterator();
			while (itMsg.hasNext()) {
				try {
					SendThread send = new SendThread(cmppProxy, itMsg.next());
					Boolean res = send.call();
					if (res == null || !res) {
						System.out.println("失败");
					} else {
						System.out.println("成功");
					}
				} catch (Exception e) {
					e.printStackTrace();
				}

			}
		} catch (UnsupportedEncodingException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

//		Thread thread = new Thread(new Runnable() {
//			@Override
//			public void run() {
//				while (true) {
//					long nowcnt = cnt.get();
//					System.out.println(nowcnt + " : " + (nowcnt - lastNum));
//					lastNum = nowcnt;
//
//					try {
//						Thread.sleep(1000);
//					} catch (InterruptedException e) {
//						// TODO Auto-generated catch block
//						e.printStackTrace();
//					}
//				}
//			}
//		});
//		thread.start();

		// CMPPSubmitMessage msg = new CMPPSubmitMessage(1,//int pk_Total,
		// 1, /*int pk_Number*/1, /*registered_Delivery*/3, /*msg_Level*/
		// "13588754", /*service_Id*/1, /*fee_UserType*/
		// "1", /*fee_Terminal_Id*/8, /*int tp_Pid*/8, /*int tp_Udhi*/
		// 8, /*int msg_Fmt*/"1358", /*String msg_Src*/"01", /*String fee_Type*/
		// "1", /*String fee_Code*/new Date(), /*Date valid_Time*/new
		// Date(),/*Date at_Time*/
		// "95598", /*String src_Terminal_Id*/new String[] { "13666672546" },
		// /*String dest_Terminal_Id[]*/
		// "test".getBytes(), /*byte msg_Content[]*/"1"/* String reserve*/
		// );
		// CMPPSubmitRepMessage rep = (CMPPSubmitRepMessage)
		// cmppProxy.send(msg);

		
//		Thread sendThread = new Thread(new Runnable() {
//			@Override
//			public void run() {
//				CMPPMsgFactory fac = new CMPPMsgFactory();
//				try {
//					List<CMPPSubmitMessage> list = fac.buildCMPPSubmitMessage("95598", new String[] { "13666672546" },
//							"12-21 10:17:35:1ABENDED GG进程状态(db2serv1)警告:" + "报警条件： REPLICAT REPOUT1 延时超过60分(747次)", 8, "", "", 3);
//					for (int i = 0; i < 10000; i++) {
//						Iterator<CMPPSubmitMessage> itMsg = list.iterator();
//						while (itMsg.hasNext()) {
//			
//							try {
//								SendThread send = new SendThread(cmppProxy, itMsg.next());
//								Boolean res = send.call();
//								if (res == null || !res) {
//									System.out.println("失败");
//								} else {
//			//						System.out.println("成功");
//								}
//							} catch (Exception e) {
//								e.printStackTrace();
//							}
//			
//						}
//					}
//				} catch (UnsupportedEncodingException e1) {
//					// TODO Auto-generated catch block
//					e1.printStackTrace();
//				}
//			}
//		});
//		
//		Thread sendThread2 = new Thread(new Runnable() {
//			@Override
//			public void run() {
//				CMPPMsgFactory fac = new CMPPMsgFactory();
//				try {
//					List<CMPPSubmitMessage> list = fac.buildCMPPSubmitMessage("95598", new String[] { "13666672546" },
//							"12-21 10:17:35:1ABENDED GG进程状态(db2serv1)警告:" + "报警条件： REPLICAT REPOUT1 延时超过60分(747次)", 8, "", "", 3);
//					for (int i = 0; i < 10000; i++) {
//						Iterator<CMPPSubmitMessage> itMsg = list.iterator();
//						while (itMsg.hasNext()) {
//			
//							try {
//								SendThread send = new SendThread(cmppProxy, itMsg.next());
//								Boolean res = send.call();
//								if (res == null || !res) {
//									System.out.println("失败");
//								} else {
//			//						System.out.println("成功");
//								}
//							} catch (Exception e) {
//								e.printStackTrace();
//							}
//			
//						}
//					}
//				} catch (UnsupportedEncodingException e1) {
//					// TODO Auto-generated catch block
//					e1.printStackTrace();
//				}
//			}
//		});
//		
//		
//		Thread sendThread3 = new Thread(new Runnable() {
//			@Override
//			public void run() {
//				CMPPMsgFactory fac = new CMPPMsgFactory();
//				try {
//					List<CMPPSubmitMessage> list = fac.buildCMPPSubmitMessage("95598", new String[] { "13666672546" },
//							"12-21 10:17:35:1ABENDED GG进程状态(db2serv1)警告:" + "报警条件： REPLICAT REPOUT1 延时超过60分(747次)", 8, "", "", 3);
//					for (int i = 0; i < 10000; i++) {
//						Iterator<CMPPSubmitMessage> itMsg = list.iterator();
//						while (itMsg.hasNext()) {
//			
//							try {
//								SendThread send = new SendThread(cmppProxy, itMsg.next());
//								Boolean res = send.call();
//								if (res == null || !res) {
//									System.out.println("失败");
//								} else {
//			//						System.out.println("成功");
//								}
//							} catch (Exception e) {
//								e.printStackTrace();
//							}
//			
//						}
//					}
//				} catch (UnsupportedEncodingException e1) {
//					// TODO Auto-generated catch block
//					e1.printStackTrace();
//				}
//			}
//		});
//		
//		sendThread.start();
//		sendThread2.start();
//		sendThread3.start();

	}


	public void  s(long temp) {
		byte[] bt = new byte[8];
		for(int i=0;i<8;i++){
			bt[i] = (byte) (temp >> (56 - i * 8));
		}

		int mask = 0xf0;
		int month =  (bt[0] & mask)>>4;
		System.out.println("Month:"+month);

		mask=0xF;
		int date =  (bt[0] & mask)<<1;
		mask=0x80;
		int date2 = bt[1]>>7;
		date = (date&(date2|0x1E));
		System.out.println("Date:"+date);

		//01111100
		mask=0x7C;
		int hour = (bt[1]&mask)>>2;
		System.out.println("Hour:"+hour);

		mask=0x3;
		int minute = (bt[1]&mask)<<4;
		minute |= (bt[2]&0xF0)>>4;
		System.out.println("Minute:"+minute);

		mask=0xf;
		int second = (bt[2]&mask)<<2;
		second |= (bt[3]&0xff)>>6;
		System.out.println("Second:"+second);

		mask = 0x3F;
		int Msg_Id = (bt[3]&mask)<<16;
		Msg_Id |= (bt[4]&0xFF)<<8;
		Msg_Id |= (bt[5]&0xFF);
		System.out.println("Msg_Id:"+Msg_Id);

		mask = 0xFF;
		int seq_id = (bt[6]&mask)<<8;
		seq_id |= (bt[7]&mask);
		System.out.println("seq_id:"+seq_id);
	}
}
